package com.bana.sys.controller;

import com.bana.sys.code.DatabaseResult;
import com.bana.sys.dao.PersonDAO;
import com.bana.sys.model.Person;
import com.bana.sys.model.PersonPageParameter;
import com.bana.sys.service.PersonService;
import com.bana.sys.utils.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping(value = "/person",method = RequestMethod.POST)
public class PersonController {

    @Resource
    private PersonDAO personDAO;

    @Resource
    private PersonService personService;

    /**
     * 查询人员集合
     * @return
     */
    @RequestMapping(value = "getPersons.do",method = RequestMethod.POST)
    public PageResponse<Person> getPersons(@RequestBody TokenRequest<PersonPageParameter> request){

        PersonPageParameter pageParameter=request.getData();
        List<Person> persons = personDAO.getPersons(pageParameter);
        int count=personDAO.getPersonCount(pageParameter);
        return new PageResponse<>(persons,count);
    }

    @RequestMapping(value = "insertPerson.do",method = RequestMethod.POST)
    public ActionResult insertPerson(@RequestBody TokenRequest<Person> request){
        personDAO.insertPerson(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        actionResult.setKey(request.getData().getRybm());
        return actionResult;
    }

    @RequestMapping(value = "updatePerson.do",method = RequestMethod.POST)
    public ActionResult updatePerson(@RequestBody TokenRequest<Person> request){
        personDAO.updatePerson(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    @RequestMapping(value = "deletePerson.do",method = RequestMethod.POST)
    public ActionResult deletePerson(@RequestBody TokenRequest<Person> request){
        personService.deletePerson(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

}
